package com.xu.uitls;

import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class DateUtil {
    /**
     * 把date转成yyyy-MM-dd格式的string
     */
    public static String gainDate(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String dateStr=sdf.format(date);
        return dateStr;
    }

    /**
     * 把date转成yyyy-MM-dd HH:mm:ss格式的string
     */
    public static String gainTime(Date date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr=sdf.format(date);
        return dateStr;
    }

    /**
     * 获取系统时间，格式为yyyy-MM-dd
     */
    public static Date getSystemDate() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        return simpleDateFormat.parse(simpleDateFormat.format(date));
    }

    /**
     * 获取系统时间，格式为yyyy-MM-dd HH:mm:ss
     */
    public static Date getSystemTime() throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        return simpleDateFormat.parse(simpleDateFormat.format(date));
    }

}
